package com.example.mobile2030606162zwh0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private CheckBox chkBoxMainYinqing;
    private CheckBox chkBoxMainBoli;
    private CheckBox chkBoxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainTip;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        chkBoxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中:"+buttonView.getText().toString());
                }else{
                    tvMainResult.setText("取消选中:"+buttonView.getText().toString());
                }
            }
        });
        chkBoxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中:"+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中:"+buttonView.getText().toString());
                }
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中:";
                if (chkBoxMainYinqing.isChecked()){
                    msg+="\n"+chkBoxMainYinqing.getText().toString();
                }
                if (chkBoxMainBoli.isChecked()){
                    msg+="\n"+chkBoxMainBoli.getText().toString();
                }
                if (chkBoxMainFangxiangpan.isChecked()){
                    msg+="\n"+chkBoxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });
    }

    private void initView() {
        chkBoxMainYinqing = (CheckBox) findViewById(R.id.chkBox_main_yinqing);
        chkBoxMainBoli = (CheckBox) findViewById(R.id.chkBox_main_boli);
        chkBoxMainFangxiangpan = (CheckBox) findViewById(R.id.chkBox_main_fangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
    }
}